﻿using System.Text;
using SVTTranslateTextEngine.Entities;
using SVTTranslateTextEngine.TTInputParams;

namespace SVTTranslateTextEngine.TTChecker
{
    /// <summary>
    /// This class to handle the rule of language information by the translation running .
    /// </summary>
    public class LanguageChecker
    {
        protected const string LANGUAGE_NOT_SUPPORTED =
            "The code as [{0}] of language is not supported by the [{1}] vendor !\r\n";

        /// <summary>
        /// check the source language is the same as target languge , or not ?
        /// </summary>
        /// <param name="szFromLang">source language code </param>
        /// <param name="szToLang">target language code </param>
        /// <returns>true or false </returns>
        public bool CheckIsTheSameLang(string szFromLang, string szToLang)
        {
            return szToLang.Equals(szFromLang);
        }

        /// <summary>
        /// check the source language is the same as target languge , or not ?
        /// </summary>
        /// <param name="textInput">the text input parameter for the transmission </param>
        /// <returns>true or false </returns>
        public bool CheckIsTheSameLang(BaseInputParam textInput)
        {
            return CheckIsTheSameLang(textInput.szFromLang, textInput.szToLang);
        }

        /// <summary>
        /// check the engine's vendor name and language code is support , or not ?
        /// </summary>
        /// <param name="szVendorName">the vendor name of translate engine </param>
        /// <param name="szLangCode">the language's code of transmission </param>
        /// <returns>error message , Empty is right </returns>
        public string CheckIsSupported(string szVendorName, string szLangCode)
        {
            string errorMessage = "";
            SupportLanguageInfo supportLangInfo = new LangHelper().GetSupportLangInfoByVendorName(szVendorName);
            bool isError = (!string.IsNullOrEmpty(szLangCode) &&
                            supportLangInfo != null && supportLangInfo.LanguageInfo != null &&
                            supportLangInfo.LanguageInfo.Get(szLangCode) != null);
            if (isError) errorMessage = string.Format(LANGUAGE_NOT_SUPPORTED, new[] {szLangCode, szVendorName});
            return errorMessage;
        }

        /// <summary>
        /// check the engine's vendor name and language code is support , or not ?
        /// </summary>
        /// <param name="input">the input parameter of the transmission </param>
        /// <returns>error message , empty is right </returns>
        public string CheckIsSupported(BaseInputParam input)
        {
            bool isTheSame = CheckIsTheSameLang(input.szFromLang, input.szToLang);
            if (isTheSame)
            {
                return "Soruce language as same as the target language , It's not allowed!\r\n";
            }
            var sbErrorMessage = new StringBuilder();
            string tempErrMsg = (CheckIsSupported(input.szVendorName, input.szFromLang));
            if (!string.IsNullOrEmpty(tempErrMsg))
                sbErrorMessage.Append("Source Language is Error : ").Append(tempErrMsg);
            tempErrMsg = (CheckIsSupported(input.szVendorName, input.szToLang));
            if (!string.IsNullOrEmpty(tempErrMsg))
                sbErrorMessage.Append("Target Language is Error : ").Append(tempErrMsg);
            return sbErrorMessage.ToString();
        }
    }
}